package com.wondersgroup.framework.sample.component.grid.action;

import com.wondersgroup.framework.core.bo.Page;
import com.wondersgroup.framework.core.web.struts2.action.BaseExcelAction;
import com.wondersgroup.framework.core.web.vo.VOUtils;
import com.wondersgroup.framework.sample.component.data.GridData;
import com.wondersgroup.framework.sample.component.grid.vo.GridBlobVO;
import com.wondersgroup.framework.sample.component.grid.vo.GridVO;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.hibernate.lob.BlobImpl;
import org.springframework.beans.BeanUtils;

public class GridPageAction extends BaseExcelAction
{
  public String query()
  {
    int start = 0;
    int size = 10;
    String pStart = this.servletRequest.getParameter("start");
    String pSize = this.servletRequest.getParameter("limit");
    if (pStart != null)
      start = Integer.parseInt(pStart);

    if (pSize != null) {
      size = Integer.parseInt(pSize);
    }

    Page page = new GridData(this.servletRequest).getPage(start, size);
    String json = VOUtils.getJsonDataFromPage(page, GridVO.class);

    this.logger.info("this is grid action: " + json);
    createJSonData(json);
    return "ajax";
  }

  public String querySummary()
  {
    int start = 0;
    int size = 10;
    String pStart = this.servletRequest.getParameter("start");
    String pSize = this.servletRequest.getParameter("limit");
    if (pStart != null)
      start = Integer.parseInt(pStart);

    if (pSize != null) {
      size = Integer.parseInt(pSize);
    }

    Page page = new GridData(this.servletRequest).getPage(0, 2147483647);
    List users = page.getResult();

    GridVO summaryVO = new GridVO();
    summaryVO.setId(String.valueOf(users.size()));
    int age = 0;
    for (Iterator i$ = users.iterator(); i$.hasNext(); ) { GridVO userVO = (GridVO)i$.next();
      age += userVO.getAge();
    }
    summaryVO.setAge(age / users.size());

    page = new GridData(this.servletRequest).getPage(start, size);
    String json = VOUtils.getJsonDataFromPage(page, GridVO.class);

    JSONObject jsonObject = JSONObject.fromObject(json);
    jsonObject.put("summary", summaryVO);
    json = jsonObject.toString();

    this.logger.info("this is grid action: " + json);
    createJSonData(json);
    return "ajax";
  }

  public List getExportDataList()
  {
    Page page = new GridData(this.servletRequest).getPage(0, 2147483647);
    List users = page.getResult();

    List result = new ArrayList();
    for (Iterator i$ = users.iterator(); i$.hasNext(); ) { GridVO userVO = (GridVO)i$.next();
      Blob image = null;
      try {
        File file = new File(this.servletRequest.getRealPath(userVO.getImage()));
        image = new BlobImpl(new FileInputStream(file), (int)file.length());
      }
      catch (Exception ex) {
        this.logger.error(ex.getMessage());
      }
      GridBlobVO blobVO = new GridBlobVO();
      BeanUtils.copyProperties(userVO, blobVO, new String[] { "image" });
      blobVO.setImage(image);
      result.add(blobVO);
    }

    return result;
  }

  public Object getExportSummaryData()
  {
    Page page = new GridData(this.servletRequest).getPage(0, 2147483647);
    List users = page.getResult();

    GridBlobVO blobVO = new GridBlobVO();
    blobVO.setId(String.valueOf(users.size()));
    int age = 0;
    for (Iterator i$ = users.iterator(); i$.hasNext(); ) { GridVO userVO = (GridVO)i$.next();
      age += userVO.getAge();
    }
    blobVO.setAge(age / users.size());
    return blobVO;
  }
}